package prv.pursuivant.render.html;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import prv.pursuivant.render.OutputGenerator;

public class XHtmlGenerator implements OutputGenerator {

	private final XMLStreamWriter writer;

	public XHtmlGenerator(XMLStreamWriter writer) {
		this.writer = writer;
	}

	@Override
	public XHtmlGenerator startDocument() {
		try {
			writer.writeStartDocument();
			writer.writeDTD("<!DOCTYPE html PUBLIC "
					+ "'-//W3C//DTD XHTML 1.0 Strict//EN' "
					+ "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

	@Override
	public XHtmlGenerator elem(String tagName) {
		try {
			writer.writeStartElement(tagName);
			if (tagName.equals("html"))
				writer.writeDefaultNamespace("http://www.w3.org/1999/xhtml");
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

	@Override
	public XHtmlGenerator emptyElem(String tagName) {
		try {
			writer.writeEmptyElement(tagName);
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

	@Override
	public XHtmlGenerator attr(String attrName, String attrValue) {
		try {
			writer.writeAttribute(attrName, attrValue);
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

	@Override
	public XHtmlGenerator text(String text) {
		try {
			writer.writeCharacters(text);
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

	@Override
	public XHtmlGenerator entRef(String name) {
		try {
			writer.writeEntityRef(name);
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

	@Override
	public XHtmlGenerator end() {
		try {
			writer.writeEndElement();
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

	@Override
	public XHtmlGenerator endDocument() {
		try {
			writer.writeEndDocument();
			return this;
		} catch (XMLStreamException e) {
			throw new UnsupportedOperationException(e);
		}
	}

}
